package cn.itcast.bos.web.action;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {


    private String recAreaInfo;
    private String sendAreaInfo;
    public void setRecAreaInfo(String recAreaInfo) {
        this.recAreaInfo = recAreaInfo;
    }
    public void setSendAreaInfo(String sendAreaInfo) {
        this.sendAreaInfo = sendAreaInfo;
    }

    //下单order_add.action
    @Action(value = "order_add",results = {@Result(name = "success",type = "redirect", location = "index.html")})
        public String orderAdd(){
        //获取寄收省市区地址信息
        Area sendArea = new Area();
        String[] send = sendAreaInfo.split("/");
        sendArea.setProvince(send[0]);
        sendArea.setCity(send[1]);
        sendArea.setDistrict(send[2]);

        Area recArea = new Area();
        String[] rec = sendAreaInfo.split("/");
        recArea.setProvince(rec[0]);
        recArea.setCity(rec[1]);
        recArea.setDistrict(rec[2]);

        model.setSendArea(sendArea);
        model.setRecArea(recArea);

        //关联当前登录用户
        Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
        model.setCustomer_id(customer.getId());

        //调用webService将数据传递bos_mgt
        WebClient.create(Constants.BOS_MANAGEMENT_URL+"/services/orderService/order")
                .type(MediaType.APPLICATION_JSON).post(model);

        return SUCCESS;
    }





 /*   Customer customer = WebClient.create(Constants.CRM_MANAGEMENT_URL + "/services/customerService/findRecAddress?recAddress=" + model.getRecAddress())
                .type(MediaType.APPLICATION_JSON).get(Customer.class);*/







}
